import { add, wechatLogin } from "@/api/Wechat";
import { addWechatUser } from "@/api/WechatUser";
import he from "element-ui/src/locale/lang/he";


export async function register(routeTo,route){
    let wechatRegister = routeTo.query.wechat_register;
    let userId = routeTo.query.user_id;
    console.log(routeTo)
    console.log('=================')
    console.log(route)
    if (wechatRegister && userId){
        console.log('执行微信注册')
        // 微信注册
        let s = decodeURIComponent(wechatRegister);
        let wechatInfo = JSON.parse(s);
        await add(wechatInfo).then(res => {
            console.log('微信注册成功')
            const wechatId = res.data.data.wechatId;
            console.log(wechatId)
            var wechatUser = {}
            wechatUser.userId = userId
            wechatUser.wechatId = wechatId
            addWechatUser(wechatUser).then(res => {
                console.log(res.data.message)
                this.$router.push({ path: '/' }).then()
            })
        })

        return true
    }

    return false
}


export function Promisify(func){
    return function (data) {
        return new Promise((resolve, reject) => {
            func
        })
    }
}


export async function login(to, from) {
    let wechatLoginParam = to.query.wechat_login;
    if (wechatLoginParam) {
        console.log('执行微信登陆')
        await wechatLogin(wechatLoginParam).then(res => {
            var header = res.headers['access-token'];
            if (header) {
                console.log('微信登陆成功')
                sessionStorage.setItem('jwt', header)
                const user = res.data.data.user;
                sessionStorage.setItem('userInfo', JSON.stringify(user))
                location.reload()
            }
        })
    }
}